package com.hitown.controller.user;

import com.hitown.model.LayuiPage;
import com.hitown.model.Result;
import com.hitown.model.user.Group;
import com.hitown.model.user.UserInfo;
import com.hitown.service.user.UserService;
import com.hitown.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
	
	@Autowired
	private UserService userSevice;
	
	@RequestMapping("getUserList")
    @ResponseBody
    public Map<String,Object> getUserList(HttpServletRequest request, LayuiPage page){
    	Map<String, Object> map = CopyUtil.copyToMap(request);
    	page.init();
    	map.put("page", page);
        return userSevice.getUserList(map);
    }
	
	@RequestMapping("addUser")
    @ResponseBody
    public Result addUser(HttpServletRequest request,UserInfo user){
    	
        return userSevice.addUser(user);
    }
	
	@RequestMapping("updateUser")
    @ResponseBody
    public Result updateUser(HttpServletRequest request,UserInfo userInfo){
       
		return userSevice.updateUser(userInfo);
    }
	
	@RequestMapping("getListGroup")
    @ResponseBody
    public Map<String,Object> getListGroup(HttpServletRequest request, LayuiPage page){
    	Map<String, Object> map = CopyUtil.copyToMap(request);
    	page.init();
    	map.put("page", page);
        return userSevice.getListGroup(map);
    }
	
	@RequestMapping("updateGroup")
    @ResponseBody
    public Result updateGroup(HttpServletRequest request){
		Map<String, Object> map = CopyUtil.copyToMap(request);
        return userSevice.updateGroup(map);
    }
	
	@RequestMapping("addGroup")
    @ResponseBody
    public Result addGroup(HttpServletRequest request,Group group){
	
        return userSevice.addGroup(group);
    }
	
	@RequestMapping("getListUserGroup")
    @ResponseBody
    public Map<String,Object> getListUserGroup(HttpServletRequest request, LayuiPage page){
    	Map<String, Object> map = CopyUtil.copyToMap(request);
    	page.init();
    	map.put("page", page);
        return userSevice.getListUserGroup(map);
    }
	
	@RequestMapping("addUserGroup")
    @ResponseBody
    public Result addUserGroup(HttpServletRequest request){
		List<String> list = new ArrayList<String>();
		Map<String, Object> map = CopyUtil.copyToMap(request);
		String ids = String.valueOf(map.get("userIds"));
		String[] id = ids.split(",");
		for (int i = 0; i < id.length; i++) {
			list.add(id[i]);
		}
		map.put("list", list);
        return userSevice.addUserGroup(map);
    }
	
	@RequestMapping("updateUserGroup")
    @ResponseBody
    public Result updateUserGroup(HttpServletRequest request){
		Map<String, Object> map = CopyUtil.copyToMap(request);
        return userSevice.updateUserGroup(map);
    }
	
}
